import java.io.Serializable;
import java.util.HashMap;

interface BaseDataStorage {
    boolean executeQuery(Query query);
    String getQueryResult(Query query);
    String execute(Query query);
    void putTable(char id, Table table);
    Table removeTable(char id);
    Table getTable(char id);
}

public class Storage implements BaseDataStorage, Serializable {
    protected HashMap<Character, Table> data = new HashMap<Character, Table>();

    public Storage() {
        data = new HashMap<Character, Table>();
    }

    public String execute(Query query) {
        if (query.getQueryType() == QueryType.GET)
            return getQueryResult(query);
        else
            return executeQuery(query) ? "OK" : "Error";
    }

    public boolean executeQuery(Query query) {
        String[] parameters = query.getQueryParameters();
        char id = parameters[0].charAt(0);
        if(!data.containsKey(id)) {
            data.put(id, new Table());
        }
        Table table = data.get(id);

        switch (query.getQueryType()) {
            case PUT:
                if(parameters.length == 2)
                    table.put(parameters[0], parameters[1]);
                break;
            case REMOVE:
                if(parameters.length == 1)
                    table.remove(parameters[0]);
                break;
            case UPDATE:
                if(parameters.length == 2)
                    table.update(parameters[0], parameters[1]);
                break;
            default:
                return false;
        }
        return true;
    }

    public String getQueryResult(Query query) {
        String[] parameters = query.getQueryParameters();

        char id = parameters[0].charAt(0);

        Table table = data.get(id);
        if(table == null)
            return "Shard with " + id + " does not exist on this server";
        String result;
        result = (parameters.length != 1) ? "Error!" : table.get(parameters[0]);
        if (result == null) {
            result = "Error!";
        }
        result = parameters[0] + " " + result;

        return result;
    }

    public Table removeTable(char id) {
        return data.remove(id);
    }

    public void putTable(char  id, Table table) {
        data.put(id, table);
    }

    public Table getTable(char  id) {
        return data.get(id);
    }

}

class Table implements Serializable {
    private HashMap<String, String> items;

    public Table() {
        items = new HashMap<String, String>();
    }

    public boolean put(String name, String phone) {
        if (!items.containsKey(name)) {
            items.put(name, phone);
            return true;
        }
        return false;
    }

    public boolean update(String name, String phone) {
        if (items.containsKey(name)) {
            items.remove(name);
            items.put(name, phone);
            return true;
        }
        return false;
    }

    public boolean remove(String name) {
        if (items.containsKey(name)) {
            items.remove(name);
            return true;
        }
        return false;
    }

    public String get(String name) {
        if (items.containsKey(name)) {
            return items.get(name);
        }
        return null;
    }
}